package com.gwsdk.paysdk.bean;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.gwsdk.open.OrderInfo;
import com.gwsdk.open.PayCallback;
import com.gwsdk.paysdk.acitvityV2.GWPayActivity;
import com.gwsdk.paysdk.acitvityV2.GWPayFriendActivity;
import com.gwsdk.paysdk.dialog.realname.GWCertificationDialog;
import com.gwsdk.paysdk.utils.GWLog;
import com.gwsdk.paysdk.utils.ToastUtil;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.dialog.AppLoadingDialog;
import com.gwsdk.paysdk.http.process.RealNameStatusProcess;
import com.gwsdk.paysdk.utils.TextUtils;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 */
public class GWPayModel {

	/**
	 * 日志打印
	 */
	private final static String TAG = "GWPayModel";

	private PayCallback pck;
	private OrderInfo currentOrderInfo = null;
	private AppLoadingDialog loadingDialog;
	private Context context;

	private static GWPayModel payModel;
	public GWRoomSeatModel seatModel;

	public static GWPayModel Instance(){
		if(null == payModel){
			payModel = new GWPayModel();
		}
		return payModel;
	}

	private GWPayModel() {
	}

	/**
	 * 商品支付
	 * @param orderInfo 商品信息
	 * @param pck 支付回调
	 */
	public void pay(final Context context, OrderInfo orderInfo, PayCallback pck) {
		this.context = context;

		if(!LoginModel.instance().isLogin()){
			ToastUtil.show(context,"用户未登录");
			return;
		}
		this.pck = pck;
		if (!checkParams(orderInfo)) {
			return;
		}
		seatModel = null;
		currentOrderInfo = orderInfo;
		showDialog(context);
		new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
			@Override
			public void run() {
				new RealNameStatusProcess(context).post(handler); //查询后台实名开关状态
			}
		}, 1000);
	}

	public void payFriend(final Context context, GWRoomSeatModel seatModel, PayCallback pck) {
		this.context = context;

		if(!LoginModel.instance().isLogin()){
			ToastUtil.show(context,"用户未登录");
			return;
		}
		if (!seatModel.isSeat){
			ToastUtil.show(context,"当前用户信息为空");
			return;
		}
		this.pck = pck;
		currentOrderInfo = null;
		this.seatModel = seatModel;
		showDialog(context);
		new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
			@Override
			public void run() {
				new RealNameStatusProcess(context).post(handler); //查询后台实名开关状态
			}
		}, 1000);
	}

	Handler handler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			disDialog();
			switch (msg.what){
				case Constant.GET_REALNAME_STATUS_SUCCESS:
					RealNamePayBean bean = (RealNamePayBean) msg.obj;
					if (bean.getRealNamePayStatus() == 1){
						if (PersonalCenterModel.getInstance().channelAndGame.getAge_status().equals("0")){
							GWLog.w(TAG, "开了实名认证支付，当前帐号没认证，弹出认证框");
							new GWCertificationDialog(context, bean.getRealNamePayMsg(), true).show();
							return;
						}
					}
					if (seatModel != null){
						Intent intent = new Intent(context, GWPayFriendActivity.class);
						context.startActivity(intent);
					}else {
						Intent intent = new Intent(context, GWPayActivity.class);
						context.startActivity(intent);
					}

					break;
				case Constant.GET_REALNAME_STATUS_FAIL:
					if (!TextUtils.isEmpty(msg.obj.toString()) && context!=null){
						ToastUtil.show(context,msg.obj.toString());
					}
					break;
			}
		}
	};

   public OrderInfo order(){
	   return currentOrderInfo;
   }

	public PayCallback getPck(String res, String payType) {
		return pck;
	}

	private void showDialog(Context context) {
		loadingDialog = new AppLoadingDialog.Builder().show(context, ((Activity)context).getFragmentManager());
	}

	private void disDialog(){
		loadingDialog.dismiss();
	}

	private boolean checkParams(OrderInfo orderInfo) {
		if (orderInfo == null){
			GWLog.e(TAG,"发起支付失败！orderInfo is null !");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getRoleId())){
			pck.callback("-1");
			GWLog.e(TAG,"发起支付失败！传入的RoleId【角色ID】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的RoleId【角色ID】为 null ！");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getRoleName())){
			GWLog.e(TAG,"发起支付失败！传入的RoleName【角色名】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的RoleName【角色名】为 null ！");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getRoleLevel())){
			GWLog.e(TAG,"发起支付失败！传入的RoleLevel【角色等级】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的RoleLevel【角色等级】为 null ！");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getGameServerId())){
			GWLog.e(TAG,"发起支付失败！传入的GameServerId【区服ID】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的GameServerId【区服ID】为 null ！");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getServerName())){
			GWLog.e(TAG,"发起支付失败！传入的ServerName【区服名】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的ServerName【区服名】为 null ！");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getProductName())){
			GWLog.e(TAG,"发起支付失败！传入的ProductName【道具名】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的ProductName【道具名】为 null ！");
			pck.callback("-1");
			return false;
		}
		if (android.text.TextUtils.isEmpty(orderInfo.getProductDesc())){
			GWLog.e(TAG,"发起支付失败！传入的ProductDesc【道具描述】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的ProductDesc【道具描述】为 null ！");
			pck.callback("-1");
			return false;
		}
//		if (android.text.TextUtils.isEmpty(orderInfo.getExtra_param())){
//			GWLog.e(TAG,"发起支付失败！传入的Extra_param【平台标识】为 null ！请传入非空参数再试");
//			ToastUtil.show("发起支付失败！传入的Extra_param【平台标识】为 null ！");
//			pck.callback("-1");
//			return false;
//		}
		if (android.text.TextUtils.isEmpty(orderInfo.getExtendInfo())){
			GWLog.e(TAG,"发起支付失败！传入的ExtendInfo【游戏透传参数】为 null ！请传入非空参数再试");
			ToastUtil.show("发起支付失败！传入的ExtendInfo【游戏透传参数】为 null ！");
			pck.callback("-1");
			return false;
		}
		GWLog.e(TAG, orderInfo.toString());
		return true;
	}

}
